package com.gobang.gobang;

public class GameBoard {
    private Boolean[][] board;
    private final int size;
    
    public GameBoard(int size) {
        this.size = size;
        board = new Boolean[size][size];
    }
    
    public Boolean getPiece(int x, int y) {
        return board[x][y];
    }
    
    public boolean placePiece(int x, int y, boolean isBlack) {
        if (board[x][y] != null) return false;
        board[x][y] = isBlack;
        return true;
    }
    
    public boolean checkWin() {
        // 检查横向
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size - 4; j++) {
                if (checkLine(i, j, 0, 1)) return true;
            }
        }
        
        // 检查纵向
        for (int i = 0; i < size - 4; i++) {
            for (int j = 0; j < size; j++) {
                if (checkLine(i, j, 1, 0)) return true;
            }
        }
        
        // 检查对角线
        for (int i = 0; i < size - 4; i++) {
            for (int j = 0; j < size - 4; j++) {
                if (checkLine(i, j, 1, 1)) return true;
                if (checkLine(i, j + 4, 1, -1)) return true;
            }
        }
        
        return false;
    }
    
    private boolean checkLine(int startX, int startY, int dx, int dy) {
        Boolean first = board[startX][startY];
        if (first == null) return false;
        
        for (int i = 1; i < 5; i++) {
            if (board[startX + i * dx][startY + i * dy] != first) {
                return false;
            }
        }
        return true;
    }
} 